<?php
/**
 * 文件缓存
 * @author qishunweb
 *
 */
class Cache {
	public $file;
	public $cachePath;
	public $separator = DIRECTORY_SEPARATOR;
	public function __construct() {
		$this->file = new File ();
		$this->cachePath =  "cache" . $this->separator . "sql" . $this->separator . date ( "Y-W" );
	}
	/**
	 * 删除缓存
	 */
	public function delSql($sqlPath) {
		$this->file->deldir ( $this->cachePath . $this->separator . md5 ( $sqlPath ) );
	}
	public function deleteIndexCache($tableName,$indexKey, $v){
		$this->file->deleteFile($this->cachePath . $this->separator. md5 ( $tableName.'info' ) .$this->separator.md5($indexKey). $this->separator . md5 ( $v ));
	}
	/**
	 * 读取SQL缓存
	 */
	public function sqlCacheRead($tableName, $sql) {
		$cachePath = $this->cachePath . $this->separator . md5 ( $tableName ) . $this->separator . md5 ( $sql );
		return $this->cacheRead($cachePath);
	}
	//**读取缓存 $cachePath缓存路径
	public function cacheRead($cachePath){
		if (file_exists ( $cachePath )) {
			$exist = unserialize ( file_get_contents ( $cachePath ) );
		} else {
			$exist = false;
		}
		return $exist;
	}
	/**
	 * 读取索引缓存
	 */
	public function indexCacheRead($tableName,$indexKey, $v) {
		$cachePath = $this->cachePath . $this->separator . md5 ( $tableName.'info' ) .$this->separator.md5($indexKey). $this->separator . md5 ( $v );
		return $this->cacheRead($cachePath);
	}
	/**
	 * 保存缓存文件
	 */
	public function cacheSave($tableName, $sql, $data) {
		$this->file->fileorFolder ( $this->cachePath . $this->separator . md5 ( $tableName ), md5 ( $sql ), serialize ( $data ) );
	}
	/**
	 * 保存索引缓存
	 */
	public function indexCacheSave($tableName,$indexKey, $v,$data){
		$this->file->fileorFolder ( $this->cachePath . $this->separator . md5 ( $tableName.'info'  ).$this->separator.md5 ( $indexKey ), md5 ( $v ), serialize ( $data ) );
	}
}
?>